import {
  getUserInfo
} from '../api/api'
export default {
  namespaced: true,
  state: {
    rtk: '',
    uid: '',
    un: '',
    UserName: '',
    UserCode: '',
    Mobile1: '',
    Mobile2: '',
    Mobile3: '',
    Phone1: '',
    Phone2: '',
    Phone3: '',
    UserType: '0', // 1=电脑，2=手机，3=手机+电脑，4=无信息权限
    PlateNumber: '',
    UserId: ''
  },
  mutations: {
    saveUserInfo (state, { rtk, uid, un }) {
      state.rtk = rtk
      state.uid = uid
      state.un = un
    },
    saveAllUserInfo (state, { UserName, UserCode }) {
      state.UserName = UserName
      state.UserCode = UserCode
    }
  },
  actions: {
    initUser ({ state, dispatch }, { uid, un, rtk }) {
      state.uid = uid
      state.un = un
      state.rtk = rtk
      return new Promise((resolve, reject) => {
        dispatch('getUserInfo').then(res => {
          if (res.status === 200) {
            const data = res.data[0]
            state.UserType = data.UserType
          } else {
            dispatch('app/showToast', {
              text: res.msg
            }, { root: true })
          }
        }, err => {
          reject(err)
        })
      })
    },
    getUserInfo ({ state, dispatch }) {
      return new Promise((resolve, reject) => {
        getUserInfo({
          rtk: state.rtk,
          uid: state.uid,
          un: state.un
        }).then(res => {
          if (res.status === 200) {
            const data = res.data[0]
            Object.assign(state, data)
          } else {
            dispatch('app/showToast', {
              text: res.msg
            }, { root: true })
          }
          resolve(res)
        }, err => {
          reject(err)
        })
      })
    }
  }
}
